﻿using B.S.BasicData.Api.Read.Application.Command;
using B.S.BasicData.Api.Read.Application.Command.RBAC;
using B.S.BasicData.Api.Read.Application.Command.User;
using B.S.BasicData.Api.Read.DTO;
using B.S.BasicData.Common;
using B.S.BasicData.Domian.Entities.Account;
using B.S.BasicData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace B.S.BasicData.Api.Read.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class AccountController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<AccountController> logger;

        public AccountController(IMediator mediator, ILogger<AccountController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 登录 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public Task<ApiResult<LoginDTO>> Login([FromQuery]LoginCommand request)
        {
            //异常处理
            try
            {
                logger.LogInformation("登录日志");
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<ShowDTO>> Show([FromQuery] ShowCommand request)
        {
            //异常处理
            try
            {
                string strnum = "12345";
                int num = strnum.Toint32();

                //获取身份证的年月日
                string idstr = "140826200405120022".GetCard();


                
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> Create(CreateCmmand request)
        {
            //异常处理
            try
            {
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                var userName=HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                var roleName=HttpContext.User.FindFirst(ClaimTypes.Role)?.Value;
                var customerValue=HttpContext.User.FindFirst("CustomClaimTypes")?.Value;
                var key=HttpContext.User.FindFirst("key")?.Value;
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<int>> Del([FromQuery]DelCommand request)
        {
            //异常处理
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> Update(UpdateCommand request)
        {
            //异常处理
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 省绑定
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<ShengModel>>> GetList([FromQuery]GetListCommand request)
        {
            //异常处理
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 市绑定
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<ShiModel>>> Shi([FromQuery] ShiCommand request)
        {
            //异常处理
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 县绑定
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<XianModel>>> Handle([FromQuery] XianCommand request)
        {
            //异常处理
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查询绑定
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<ShowDTO>>> GetBu([FromQuery]GetBuCommand request)
        {
            //异常处理
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示用户角色
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<UserDTO>> UserShow([FromQuery]UserShowCommand request)
        {
            //异常处理
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
